﻿<html>
	<head>
		<title>Hirdetés összeállítása</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<link rel="stylesheet" href="v-a.css" type="text/css">
	</head>
	<?php	
		if ($debug){
		error_reporting(E_ALL);
		ini_set('display_errors', '1');
		}
		session_start();
		//print_r ($_SESSION['editedhirdetes']);
		require_once ('dbConnect.php');
		include ('functions.php');
		
		if(isset($_POST['Vissza'])) {
			$_SESSION['uzenet']="";
			header('Location: '.$_SESSION['vissza']);
			exit;
		}
		if(isset($_POST['Frissit'])) {
			if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 

			}
			// Temporary file name stored on the server
			$tmpName  = $_FILES['image']['tmp_name'];  
           
			// Read the file 
			$fp     = fopen($tmpName, 'r');
			$data = fread($fp, filesize($tmpName));
			$data = addslashes($data);
			fclose($fp);
			$result=MyDatabase::getConnection()->multi_query("CALL updatetermeny('".$_SESSION['editedtermeny']."','".$_POST['termenynameTB']."','".$_POST['mertekegysegCB']."','".$data."')");
			
			if (!$result) {
				$_SESSION['uzenet']='mysql_error: ' . MyDatabase::getConnection()->error;
			}
			else{
				$_SESSION['uzenet']='Frissítve';
				
			}
		}
		if(isset($_POST['új'])) {
			//echo $_FILES['image']['tmp_name'];
			//exit;
			if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 

			}
			// Temporary file name stored on the server
			$tmpName  = $_FILES['image']['tmp_name'];  
           
			// Read the file 
			$fp     = fopen($tmpName, 'r');
			$data = fread($fp, filesize($tmpName));
			$data = addslashes($data);
			fclose($fp);
			
			//echo $_POST['mennyisegTB'];
			$result=MyDatabase::getConnection()->multi_query("CALL ujhirdetes('".$_POST['mennyisegTB']."','".$_POST['termenyCB']."','".$_POST['egysegarTB']."','".$_POST['minTB']."','".$_SESSION['username']."','".$data."')");
			
			if (!$result) {
				$_SESSION['uzenet']='mysql_error: ' . MyDatabase::getConnection()->error;
			}
			else{
				$_SESSION['uzenet']='új hirdetés felvéve';
			}
		}
		if(!isset($_POST['új'])) {
			//$row=termenyedit($_SESSION['editedtermeny']);
			//$_POST['termenynameTB']=$row[1];
			//$_POST['mertekegysegCB']=$row[2];
		}
		
	?>	
	<body>
	
		<table ><tr HEIGHT="10"><td WIDTH="80%">Hirdetés összeállítása</td><td WIDTH="20%"><?php include('kilepes.php');?></td></tr></table>
		
		<hr>
		
		<?php 
			printf($_SESSION['uzenet']);
		?>
		<hr>
		<form enctype="multipart/form-data" method='post' action="hirdetesedit.php">
		<table>
		<tr>
		<td>
		<input type='submit'  class='button' name='Vissza' value='<< vissza' />
		<?php if($_SESSION['editedhirdetes']!=""): ?>
			<input type='submit'  class='button' name='Frissit' value='frissít' />
		<?php endif; ?>
		</td><td>
		<input type='submit'  class='button' name='új' value='új' />
		</td>
		</tr>
		</table>
			<table width='400px'>
				<tr>
					<td>
						<label>termény</label>
					</td>
					<td>
						<select name='termenyCB'>
							<?php
							$query="CALL termenyek();";
							if (MyDatabase::getConnection()->multi_query($query)) {
									if ($result = MyDatabase::getConnection()->store_result()) {
										$j=0;
										while ($row = $result->fetch_row()) {
												echo "<option value=$row[0] ";
												if ($_POST['termenyCB']==$row[0]) {echo "selected='selected' ";}
												echo">$row[1]---$row[2]</option>";
											$j++;
										}
										$result->free();
									}
							}
							else{
								$_SESSION['uzenet']= MyDatabase::getConnection()->error;
								//printf(MyDatabase::getConnection()->error);
							}
							?>

						</select>
					</td>
					
				</tr>
				<tr>
					<td>
						<label>mennyiség:</label>
					</td>
					<td>
						<input  type='text' name='mennyisegTB' maxlength='20' size='20' value='<?php 
							if(isset($_POST['mennyisegTB']))
								print $_POST['mennyisegTB'];
						?>'>
					</td>
				</tr>
				<tr>
					<td>
						<label>egységár:</label>
					</td>
					<td>
						<input  type='text' name='egysegarTB' maxlength='20' size='20' value='<?php 
							if(isset($_POST['egysegarTB']))
								print $_POST['egysegarTB'];
						?>'>
					</td>
				</tr>
				<tr>
					<td>
						<label>minimális rendelés:</label>
					</td>
					<td>
						<input  type='text' name='minTB' maxlength='20' size='20' value='<?php 
							if(isset($_POST['minTB']))
								print $_POST['minTB'];
						?>'>
					</td>
				</tr>

			</table>
			<input name="MAX_FILE_SIZE" value="102400" type="hidden">
			<input name="image" accept="image/jpeg" type="file">

		</form>
		
	</body>
</html>

